﻿namespace Whut.ObjectSharePointMapper.Internals
{
    using System;
    using System.Reflection;
    using Microsoft.SharePoint;

    public class SessionFactory : ISessionFactory
    {
        private MetaDataStore metaDataStore;

        private SessionFactory(MetaDataStore metaDataStore)
        {
            this.metaDataStore = metaDataStore;
        }

        public static ISessionFactory Create(Assembly assembly, params Assembly[] assemblies)
        {
            var sessionFactory = new SessionFactory(new MetaDataStore());
            sessionFactory.metaDataStore.BuildMetadataFor(assembly);
            if (assemblies != null)
            {
                for (int i = 0; i < assemblies.Length; i++)
                {
                    sessionFactory.metaDataStore.BuildMetadataFor(assemblies[i]);
                }
            }

            return sessionFactory;
        }

        public ISession CreateSession(SPWeb web)
        {
            return new Session(web, this.metaDataStore);
        }

        public ISharePointSession CreateSharePointSession()
        {
            return new SharePointSession(this.metaDataStore);
        }
    }
}
